package pe.com.cotelasa.scp.controlador.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import pe.com.cotelasa.scp.dominio.service.IServicioUbigeo;
import pe.com.cotelasa.scp.entidad.Pais;

/**
 * @author zer0
 */
@FacesConverter(value = "paisConverter")
@Component("paisConverter")
public class PaisConverter implements Converter {

    @Inject
    private IServicioUbigeo servicioUbigeo;

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String nombrePais) {
        try {
            return servicioUbigeo.obtenerPaisPorNombre(nombrePais);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        
        if (o == null || o.equals("")) {  
            return "";  
        }else{
            return String.valueOf(((Pais) o).getNombre()); 
        }
    }
}
